<?php
/**
 * Created by PhpStorm.
 * @author: lizhi <lizhi7@xin.com>
 * Date: 2018/4/5 17:25
 */

namespace app\controllers;
use app\models\Feed;
use app\models\Pet;
use app\models\UserPet;
use yii\helpers\Url;
use yii\data\Pagination;
use yii\web\Controller;
use app\models\form\PetSearchForm;
use Yii;

class PetController extends Controller
{

    /**
     * @author lizhi7  <lizhi7@xin.com>
     * @return string|\yii\web\Response
     */
    public $enableCsrfValidation = false;
    public function actionDetail()
    {
        if (Yii::$app->user->isGuest) {
            return $this->goHome();
        }
        $pid = Yii::$app->request->get('pid');
        if (empty($pid)) {
            return $this->asJson(['code' => 10002, 'msg' => '请选择您的宠物id']);
        }
        $userPet = new UserPet();
        $Feed = new Feed();
        $userId = Yii::$app->user->getId();
        $userPetRow = $userPet->find()->where('user_id=:user_id and pid=:pid and ascription_status=1',
            [':user_id' => $userId, ':pid' => $pid])
            ->with('pet')->orderBy('puid', 'desc')->one();
        if (empty($userPetRow)) {
            return $this->asJson(['code' => 10003, 'msg' => '请选择您的宠物']);
        }
        $historyFeedList = $Feed->getHistoryFeed($userId, $pid, 20);
        return $this->render('detail', ['userPetRow' => $userPetRow, 'historyFeedList' => $historyFeedList]);
    }

    /**
     * 挂起
     * @return \yii\web\Response
     */
    public function actionHang()
    {
        if (Yii::$app->user->isGuest) {
            return $this->asJson(['code' => 10001, 'msg' => '您还没有登录']);
        }
        $pid = Yii::$app->request->post('pid');
        $price = Yii::$app->request->post('price');
        if (empty($pid) || empty($price)) {
            return $this->asJson(['code' => 10001, 'msg' => '参数错误']);
        }
        if($price <= 0) {
            return $this->asJson(['code' => 10002, 'msg' => '挂卖价格必须大于0的整数']);
        }
        $userId = Yii::$app->user->getId();
        $userPet = new UserPet();
        $userPetRow = $userPet->find()->where('user_id=:user_id and pid=:pid and ascription_status=1',
            [':user_id' => $userId, ':pid' => $pid])->one();
        $userPetRow->auction_status = 1;
        $userPetRow->price = $price;
        $userPetRow->save();
        return $this->asJson(['code' => 10000, 'msg' => '挂卖成功']);
    }

    /**
     * 取消挂起
     * @return \yii\web\Response
     */
    public function actionCancelHang()
    {
        if (Yii::$app->user->isGuest) {
            return $this->asJson(['code' => 10001, 'msg' => '您还没有登录']);
        }
        $pid = Yii::$app->request->post('pid');
        if (empty($pid)) {
            return $this->asJson(['code' => 10002, 'msg' => '参数错误']);
        }
        $userId = Yii::$app->user->getId();
        $userPet = new UserPet();
        $userPetRow = $userPet->find()->where('user_id=:user_id and pid=:pid and ascription_status=1',
            [':user_id' => $userId, ':pid' => $pid])->one();
        $userPetRow->auction_status = 0;
        $userPetRow->price = 0;
        $userPetRow->save();
        return $this->asJson(['code' => 10000, 'msg' => '取消挂卖成功']);
    }

    /**
     * 挂卖详情
     * @return \yii\web\Response
     */
    public function actionHangDetail()
    {
        $userId = Yii::$app->request->get('user_id');
        $pid = Yii::$app->request->get('pid');
        $myUserId = Yii::$app->user->getId();
        $userPet = new UserPet();
        $userPetRow = $userPet->find()->where('user_id=:user_id and pid=:pid and ascription_status=1',
            [':user_id' => $userId, ':pid' => $pid])->one();
        if (empty($userPetRow)) {
            return $this->asJson(['code' => 10003, 'msg' => '请选择您的宠物']);
        }
        $historyFeedList = (new Feed())->getHistoryFeed($userId, $pid, 20);
        return $this->render('hang-detail', [
            'userPetRow' => $userPetRow, 'historyFeedList' => $historyFeedList,
            'myUserId' => $myUserId, 'userId' => $userId
        ]);
    }

    /**
     * 挂卖列表
     * @return string
     */
    public function actionList()
    {
        $userPet = new UserPet();
        $petSearch = new PetSearchForm();
        $query = $userPet->getSearchQuery();
        //var_dump(Yii::$app->request->getQueryParams());exit;
        $query = $petSearch->loadSearch($query);
        $countQuery = clone $query;
        $params = $petSearch->getParams();
        $pages = new Pagination([
            'totalCount' => $countQuery->count(),
            'params' => $params
        ]);
        $pages->setPageSize(4);
        $list = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();
        return $this->render('list', [
            'list' => $list, 'pages' => $pages,
            'params' => $params, 'sortUrl' =>  $petSearch->getSortUrl()
        ]);
    }

    /**
     * 拍卖场
     * @return string
     */
    public function actionAuctionList()
    {
        $list = (new Pet())->getAuctionList();
        return $this->render('auction-list', [
            'list' => $list,
        ]);
    }
}